﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AvalonRegionAdapter.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="AvalonRegionAdapter" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Windows.Client.Adapters
{
    using System.Collections.Specialized;
    using System.Windows;

    using AvalonDock;
    using Microsoft.Practices.Prism.Regions;

    /// <summary>
    /// Defines the <seealso cref="AvalonRegionAdapter"/> type.
    /// </summary>
    public sealed class AvalonRegionAdapter : RegionAdapterBase<DocumentPane>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="AvalonRegionAdapter"/> class.
        /// </summary>
        /// <param name="factory">The factory.</param>
        public AvalonRegionAdapter(IRegionBehaviorFactory factory)
            : base(factory)
        {
        }

        /// <summary>
        /// Adapts the specified region.
        /// </summary>
        /// <param name="region">The region.</param>
        /// <param name="regionTarget">The region target.</param>
        protected override void Adapt(IRegion region, DocumentPane regionTarget)
        {
            region.Views.CollectionChanged += (sender, e) => OnViewsCollectionChanged(sender, e, region, regionTarget);
        }

        /// <summary>
        /// Creates the region.
        /// </summary>
        /// <returns>Avalon dock region</returns>
        protected override IRegion CreateRegion()
        {
            return new AllActiveRegion();
        }

        /// <summary>
        /// Called when [views collection changed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Collections.Specialized.NotifyCollectionChangedEventArgs"/> instance containing the event data.</param>
        /// <param name="region">The region.</param>
        /// <param name="regionTarget">The region target.</param>
        private static void OnViewsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e, IRegion region, DocumentPane regionTarget)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                foreach (object item in e.NewItems)
                {
                    var view = item as UIElement;

                    if (view != null)
                    {
                        var newContentPane = new DockableContent { Content = item, Title = view.GetType().ToString() };
                        newContentPane.Closed += (contentPaneSender, args) =>
                        {
                        };
                        regionTarget.Items.Add(newContentPane);
                        newContentPane.Activate();
                    }
                }
            }
            else
            {
                if (e.Action == NotifyCollectionChangedAction.Remove)
                {
                }
            }
        }
    }
}